#!/bin/bash
# doris部署环境要求：
#（1）一台或多台机器，操作系统CentOS 7.x-86_x64
#（2）硬件配置：内存4G+，CPU 2核+；
#（3）集群内各个机器之间能相互通信；
#（4）集群内各个机器可以访问外网，需要拉取文件；
#（5）提前安装 yum install -y  wget

# 安装步骤
#1. 安装jdk 1.8.0_202
#2. 安装doris
#2.1 初始化环境
#2.2 下载 apache-doris-2.0.0-bin-x64.tar.gz
#2.3 安装 FE 
#2.4 安装 BE

set -e

# 安装日志
install_log=/var/log/install_k8s.log
tm=$(date +'%Y%m%d %T')

# 日志颜色
COLOR_G="\x1b[0;32m"  # green
RESET="\x1b[0m"

function info(){
    echo -e "${COLOR_G}[$tm] [Info] ${1}${RESET}"
}

function run_cmd(){
  sh -c "$1 | $(tee -a "$install_log")"
}

function run_function(){
  $1 | tee -a "$install_log"
}

function install_java(){
  yum install -y wget

  info "1.自动下载jdk..."
  wget --no-check-certificate https://repo.huaweicloud.com/java/jdk/8u202-b08/jdk-8u202-linux-x64.tar.gz
  
  info "2.解压到/usr/local/java文件夹..."
  mkdir /usr/local/java/
  tar -zxvf jdk-8u202-linux-x64.tar.gz -C /usr/local/java
  
  info "3.配置环境变量..."
  echo "export JAVA_HOME=/usr/local/java/jdk1.8.0_202" >> /etc/profile
  echo "export JRE_HOME=${JAVA_HOME}/jre" >> /etc/profile
  echo "export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib" >> /etc/profile
  echo "export PATH=${JAVA_HOME}/bin:$PATH" >> /etc/profile
  source /etc/profile
  ln -s /usr/local/java/jdk1.8.0_202/bin/java /usr/bin/java

  info "4.测试 java 是否安装正确..."
  java -version
}

function install_doris() {
    info "初始化doris部署环境..."
	read -p "是否初始化 doris部署环境？默认为：no. Enter [yes/no]：" is_init_dev
    if [[ "$is_init_dev" == 'yes' ]];then
      run_function "init_env"
    fi
	
    info "下载doirs..."
    download_doris
    
    read -p "是否安装doris fe？默认为：no. Enter [yes/no]：" is_doris_fe
    if [[ "$is_doris_fe" == 'yes' ]];then
      run_function "install_doris_fe"
    fi

    read -p "是否安装doris be？默认为：no. Enter [yes/no]：" is_doris_be
    if [[ "$is_doris_be" == 'yes' ]];then
      run_function "install_doris_be"
    fi
}

# 初始化部署环境
function init_env() {
  info "关闭防火墙"
  systemctl stop firewalld
  systemctl disable firewalld

  info "关闭selinux"
  sed -i 's/^SELINUX=enforcing$/SELINUX=disabled/g' /etc/selinux/config
  source /etc/selinux/config

  info "关闭swap（k8s禁止虚拟内存以提高性能）"
  swapoff -a
  sed -i '/swap/s/^\(.*\)$/#\1/g' /etc/fstab

  info "设置文件句柄数"
  echo "* soft nofile 204800" >> /etc/security/limits.conf
  echo "* hard nofile 204800" >> /etc/security/limits.conf
  echo "* soft nproc 204800" >> /etc/security/limits.conf
  echo "* hard nproc 204800" >> /etc/security/limits.conf
  echo fs.file-max = 6553560 >> /etc/sysctl.conf

  info "设置max_map_count"
  echo vm.max_map_count = 2000000 >> /etc/sysctl.conf
  sysctl -w vm.max_map_count=2000000
}

function download_doris() {
  yum install -y wget

  info "1.下载Doris 2.0.0 tar.gz 文件..."
  if [[ ! -f "/root/apache-doris-2.0.0-bin-x64.tar.gz" ]]; then
     wget --no-check-certificate https://apache-doris-releases.oss-accelerate.aliyuncs.com/apache-doris-2.0.0-bin-x64.tar.gz
  fi
  
  info "2.解压文件/opt/doris文件夹下..."
  if [[ ! -d "/opt/doris/" ]]; then
     mkdir /opt/doris/
  fi
  tar -zxvf apache-doris-2.0.0-bin-x64.tar.gz --strip-components 1 -C /opt/doris/
}

function install_doris_fe() {
  sleep 1
  read -p "请输入priority_networks 参数：" fe_ip
  eval sed -i '58i\priority_networks=${fe_ip}/24' /opt/doris/fe/conf/fe.conf
  
  info "FE 启动ing..."
  sudo sh /opt/doris/fe/bin/start_fe.sh --daemo

  info "设置FE自动启动..."
  touch /etc/init.d/apache-doris-fe
  echo '#!/bin/bash' >> /etc/init.d/apache-doris-fe
  echo '# chkconfig: 2345 25 75' >> /etc/init.d/apache-doris-fe
  echo '/bin/bash /opt/doris/fe/bin/start_fe.sh --daemo' >> /etc/init.d/apache-doris-fe
  
  chmod +x /etc/init.d/apache-doris-fe
  chkconfig --add apache-doris-fe
  
  info "检查FE 是否已启动(需等待一段时间，失败可忽略)..."
  curl http://127.0.0.1:8030/api/bootstrap
}

function install_doris_be() {

  sleep 1
  read -p "请输入priority_networks 参数：" be_ip
  eval sed -i '58i\priority_networks=${be_ip}/24' /opt/doris/be/conf/be.conf
  sed -i '28i\JAVA_HOME=/usr/local/java/jdk1.8.0_202' /opt/doris/be/conf/be.conf
  
  info "BE 启动ing..."
  mv /opt/doris/extensions/* /opt/doris/be/lib/
  sudo sh /opt/doris/be/bin/start_be.sh --daemo

  info "设置BE自动启动..."
  touch /etc/init.d/apache-doris-be
  echo '#!/bin/bash' >> /etc/init.d/apache-doris-be
  echo '# chkconfig: 2345 50 50' >> /etc/init.d/apache-doris-be
  echo '/bin/bash /opt/doris/be/bin/start_be.sh --daemo' >> /etc/init.d/apache-doris-be
  
  chmod +x /etc/init.d/apache-doris-be
  chkconfig --add apache-doris-be
  
  info "检查BE 是否已启动(需等待一段时间，失败可忽略)..."
  curl http://127.0.0.1:8040/metrics
}


# 安装jdk
read -p "是否安装jdk 1.8.0_202？默认为：no. Enter [yes/no]：" is_jdk
if [[ "$is_jdk" == 'yes' ]];then
  run_function "install_java"
fi

# 安装k8s
read -p "是否安装is_doris？默认为：no. Enter [yes/no]：" is_doris
if [[ "$is_doris" == 'yes' ]];then
  run_function "install_doris"
fi